<?php
/**
 * Created by PhpStorm.
 * User: zlkmu
 * Date: 2017/8/30
 * Time: 16:36
 */

    require_once ('../SQLBase.php');
    require_once ('../Common.php');
    class FinanceFeeManage{
        //辅助函数，获取所有部门经费
        function getAllSectionFinanceFee($section_id, $section_type){
            $conn = new SQLBase();
            $query = "SELECT section_fee.*, allsection.section_name, allsection.section_type FROM section_fee, allsection WHERE
                    section_fee.section_ID = allsection.section_ID AND allsection.section_state = 1";
            if($section_type != 0){
                $query .= " AND allsection.section_type = '$section_type'";
            }

            if($section_id != 0){
                $query .= " AND section_fee.section_ID = '$section_id'";
            }
            //echo $query;

            $r = $conn->queryData($query);
            $result_array = array();
            if($r){
                $result_array['state'] = 1;
                $result_array['data'] = array();
                for($i = 0 ; $i < $r->num_rows; $i++){
                    $row = $r->fetch_array();
                    $result_array['data'][$i] = array('section_id'=>$row['section_ID'],
                        'section_name'=>$row['section_name'], 'section_type'=>$row['section_type'],
                        'admin_fee'=>$row['admin_fee'], 'work_fee'=>$row['work_fee'],
                        'other_fee'=>$row['other_fee'], 'total_fee'=>$row['total_fee']);
                }
                $result_array['total_count'] = $r->num_rows;
            }
            else{
                $result_array['state'] = 0;
                $result_array['error'] = 'DB_ERROR';
            }
            return $result_array;
        }


        //辅助函数，追加部门经费
        function addSectionFinanceFee($fee_data){
            $admin_fee = $fee_data['admin_fee'];
            $work_fee = $fee_data['work_fee'];
            $other_fee = $fee_data['other_fee'];
            $total_fee = $fee_data['total_fee'];
            $section_id = $fee_data['section_id'];
            $conn = new SQLBase();
            $query = "UPDATE section_fee SET admin_fee = admin_fee + $admin_fee,
                                          work_fee = work_fee + $work_fee,
                                          other_fee = other_fee + $other_fee,
                                          total_fee = total_fee + $total_fee WHERE section_ID = $section_id";
            $r = $conn->queryData($query);
            if($r){
                return true;
            }
            else{
                return false;
            }
        }

        //辅助函数，判断是否有足够经费进行报销
        function isEnoughtSectionFeeToCheck($sectionID, $fee_detail){
            $conn = new SQLBase();
            $t_fee = array('admin_fee'=>0, 'work_fee'=>0, 'other_fee'=>0);
            //计算当前待报销的总经费
            for($i = 0; $i < count($fee_detail); $i++){
                $fee_id = $fee_detail[$i]['fee_type'];
                $money = $fee_detail[$i]['money'];
                $query = "SELECT fee_item.item_type FROM fee_item WHERE item_id = $fee_id";
                $r = $conn->queryData($query);
                $fee_type = $r->fetch_array()['item_type'];
                switch($fee_type){
                    case 0:
                        $t_fee['admin_fee'] += $money;
                        break;
                    case 1:
                        $t_fee['work_fee'] += $money;
                        break;
                    case 2:
                        $t_fee['other_fee'] += $money;
                        break;
                }
            }
            //获取当前部门经费
            $query = "SELECT * FROM section_fee WHERE section_ID = $sectionID";
            $r = $conn->queryData($query);
            $row = $r->fetch_array();
            if( ($row['admin_fee'] - $t_fee['admin_fee'] < 0) || ($row['work_fee'] - $t_fee['work_fee'] < 0)
               || ($row['other_fee'] - $t_fee['other_fee'] < 0)){
                return false;
            }
            else{
                return true;
            }
        }

        //辅助函数，核销部门经费
        function subSectionFee($checkedFeeID){
            $conn = new SQLBase();
            $query = "SELECT ordinary_fee_detail.*, ordinary_fee_approve.section_ID FROM ordinary_fee_detail, fee_item, ordinary_fee_approve 
                    WHERE ordinary_fee_detail.fee_id = '$checkedFeeID' AND ordinary_fee_detail.fee_type = fee_item.item_id
                    AND ordinary_fee_detail.fee_id = ordinary_fee_approve.fee_id AND fee_item.is_section = 1";
           // echo $query;
            $r = $conn->queryData($query);
            if(!$r){
                return false;
            }
            else{
                for($i = 0; $i < $r->num_rows; $i++){
                    $row = $r->fetch_array();
                    $fee_type = $row['item_type'];
                    $fee = $row['fuhe_money'];
                    $sID = $row['section_ID'];
                    switch($fee_type){
                        case 0:
                            $query = "UPDATE section_fee SET admin_fee = admin_fee - $fee,
                                      total_fee = total_fee - $fee WHERE section_ID = $sID";
                            break;
                        case 1:
                            $query = "UPDATE section_fee SET work_fee = work_fee - $fee,
                                      total_fee = total_fee - $fee WHERE section_ID = $sID";
                            break;
                        case 2:
                            $query = "UPDATE section_fee SET work_fee = work_fee - $fee,
                                      total_fee = total_fee - $fee WHERE section_ID = $sID";
                            break;
                    }
                    $r = $conn->queryData($query);
                    if(!$r){
                        return false;
                    }
                }
                return true;
            }
        }
        //辅助函数，获取部门报销经费情况
        function getSectionApproveFee($section_id, $fee_type, $begin_time, $end_time){

            $conn = new SQLBase();
            $section_fee_info = array();

            if($fee_type == -1 || $fee_type == 0) //如果是全部或者仅差旅
            {
                //获取差旅报销情况
                $query = "SELECT finance_approve_business_fee.* FROM finance_approve_business_fee WHERE section_id = $section_id
                    AND check_time >= '$begin_time' AND check_time <= '$end_time'";

                $r = $conn->queryData($query);
                if($r){
                    for($i = 0; $i < $r->num_rows; $i++){
                        $row = $r->fetch_array();
                        array_push($section_fee_info, array('type'=>1, 'fee'=>$row['total_fee'], 'check_time'=>$row['check_time'], 'is_section'=>'否'));
                    }
                    if($fee_type == 0){ //如果是仅差旅，就直接返回数据
                        $result_array = array('state'=>1);
                        $result_array['data'] = $section_fee_info;
                        $result_array['total_count'] = count($section_fee_info);
                        exit(json_encode($result_array));
                    }
                }
                else{
                    exit(json_encode(array('state'=>0, 'error'=>'TRAVEL_FEE_ERROR')));
                }
            }
            //如果是全部类型，或者是特定类型
            //获取业务报销情况
            $query = "SELECT ordinary_fee_detail.*, fee_item.item_name, fee_item.is_section, finance_approve_ordinary_fee.fuhe_time FROM ordinary_fee_detail, finance_approve_ordinary_fee, ordinary_fee_approve, fee_item
                    WHERE ordinary_fee_approve.section_ID = $section_id 
                    AND ordinary_fee_approve.fee_id = ordinary_fee_detail.fee_id
                    AND ordinary_fee_approve.fee_id = finance_approve_ordinary_fee.approve_id
                    AND finance_approve_ordinary_fee.fuhe_time >= '$begin_time' 
                    AND finance_approve_ordinary_fee.fuhe_time <= '$end_time' 
                    AND ordinary_fee_detail.fee_type = fee_item.item_id";

            if($fee_type > 0) //如果是某一个特定的报销类别
            {
                $query .= "AND ordinary_fee_detail.fee_type = $fee_type";
            }
            $r = $conn->queryData($query);
            if($r) {
                for ($i = 0; $i < $r->num_rows; $i++) {
                    $row = $r->fetch_array();
                    array_push($section_fee_info,array('type' => 2, 'fee' => $row['fuhe_money'],
                        'fee_type' => $row['fee_type'], 'fee_name' => $row['item_name'],
                        'check_time'=>$row['fuhe_time'], 'is_section'=>($row['is_section'] == 1 ? '是':'否')));
                }
            }
            else{
                exit(json_encode(array('state'=>0, 'error'=>'ORDINARY_FEE_ERROR')));
            }

            $result_array = array('state'=>1);
            $result_array['data'] = $section_fee_info;
            $result_array['total_count'] = count($section_fee_info);
            echo json_encode($result_array);
        }



    }




